<?php

namespace App\Dao;

use App\Model\Setting;
use App\Utils\MemcacheClient;
use Hyperf\Context\ApplicationContext;

class SettingDao
{
    private static $instance;

    public static function getInstance()
    {
        if (!self::$instance) {
            self::$instance = Applicationcontext::getcontainer()->get(self::class);
        }
        return self::$instance;
    }

    /**
     * 缓存信息
     * @return array $data
     */
    public function getData()
    {
        $data = MemcacheClient::getInstance()->get('seeklove_setting_data');
        if (empty($data)) {
            $data = Setting::query()->first();
            MemcacheClient::getInstance()->set('seeklove_setting_data', $data, 180);
        }
        return $data;
    }

    public function getOne($name, $data = null)
    {
        $data = empty($data) ? $this->getData() : $data;
        return $data[$name] ?? 0;
    }

}